package com.dhcc.DRGsHosp.application.impl;

import com.dhcc.DRGsHosp.application.MrOperationApplication;
import com.dhcc.DRGsHosp.core.domain.MrOperation;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 手术信息 的 数据访问层--实体类
 * @ClassName: MrOperationApplicationImpl  
 * @Description ： 手术信息 数据访问层 实体类   
 * @Date: 2018年7月26日 下午9:46:13  
 * @Author QiaoYiLong 
 * @Version 3.1.1 DHC-DRG
 */

@Named("drgsHospMrOperationApplicationImpl20181212")
@Transactional
public class MrOperationApplicationImpl implements MrOperationApplication {

	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;

	private QueryChannelService queryChannel;

	private QueryChannelService getQueryChannelService(){
		if(queryChannel==null){
			queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
		}
		return queryChannel;
	}

	/**
	 * @param condition
	 * @return
	 * @throws
	 * @Description: 查询手术信息
	 * @author chenhanlin
	 * @date 2018/7/29 15:51
	 */
	@Override
	public List<MrOperation> queryMrOperationByCondition(Map<String, Object> condition) {
		// 分页sql
		String pageSqlBody = " ( SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM ( SELECT a.PMAININDEX FROM  drgs.MR_BASEINFO a LEFT JOIN drgs.MR_IPINFO ipinfo ON a.PMAININDEX = ipinfo.PMAININDEX WHERE   1=1  ORDER BY ipinfo.PMAININDEX desc) PAGERESULT ) WHERE rn>(:pageNo-1)*:pageSize and rn <=:pageNo*:pageSize )page" ;

		StringBuffer sqlb = new StringBuffer() ;
		sqlb.append(" select ID \"id\", o.PMAININDEX \"pmainIndex\", POTYPE \"poType\", POINDEX \"poIndex\", PO0100 \"po0100\", PO0101 \"po0101\", PO0200 \"po0200\", PO0300 \"po0300\", PO0301 \"po0301\", PO0400 \"po0400\", PO0401 \"po0401\", PO0500 \"po0500\", PO0501 \"po0501\", PO0600 \"po0600\", PO0601 \"po0601\", PO0700 \"po0700\", PO0701 \"po0701\", PO0800 \"po0800\", PO0801 \"po0801\", PO0900 \"po0900\", PO0901 \"po0901\", PO1000 \"po1000\", PO1001 \"po1001\", PO1100 \"po1100\", PO1200 \"po1200\", PO1201 \"po1201\", PO1300 \"po1300\", PO1301 \"po1301\", PO1400 \"po1400\", PO1500 \"po1500\", PO1501 \"po1501\", PO0201 \"po0201\"\n") ;
		sqlb.append(" FROM\n" ) ;
		sqlb.append(" drgs.MR_OPERATION o\n" ) ;
		sqlb.append(" right join "+pageSqlBody+" ON page.PMAININDEX = o.PMAININDEX \n" ) ;
		String sql = sqlb.toString() ;
		// 时间条件
		if(condition.get("dateFrom")!=null && condition.get("dateTo") !=null){
			String dateField = "" ;
			// 时间字段
			if("outDate".equals(condition.get("dateType"))){ // 出院日期
				dateField = "ipinfo.P00290000" ;
			} else if("pushDate".equals(condition.get("dateType"))) { // 推送日期
				dateField = "a.push_date" ;
			}
			sql = sql.replace("1=1", " 1=1 and to_char("+dateField+",'yyyy-mm-dd')  BETWEEN :dateFrom AND :dateTo ") ;
		}

		if(condition.get("hospCode") != null){
			sql = sql.replace("1=1" ," 1=1 and instr(:hospCode,a.p00010001)>0 ") ;
		}

		List<MrOperation> mrOperationList = jdbcTempSeverice.query(sql,condition,new BeanPropertyRowMapper<MrOperation>(MrOperation.class));
		return mrOperationList;
	}


	/**  
	 *  功能描述：取得主页索引为pmainIndex的所有 手术信息
	 *  @Author QiaoYiLong
	 *  @param pmainIndex
	 *  @Date:2018年7月26日下午9:44:21
	 */
	@Override
	public List<MrOperation> getMrOperationsByPmainIndex(String pmainIndex) {
		if( pmainIndex == null || "".equals(pmainIndex) ){
			return null;
		}

		try{
			String jpql = " select _operation from MrOperation _operation where _operation.pmainIndex = '"+pmainIndex+"' ";
			List<MrOperation> all = getQueryChannelService().createJpqlQuery(jpql).list();
			return all;
		}catch(Exception ex){
			ex.printStackTrace();
			return new ArrayList<>();
		}
	}

}
